home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-04-20 | 3.5 KB | 134 lines |
- package client;
-
- import java.io.*;
- import java.net.*;
- import server.Server;
-
- /**
- * Communicates with the database server.
- * @see server.Server
- * @version 1.0
- * @author Barry Boone
- */
- public class Client {
-
- /* Data stream to read from server. */
- DataInputStream remoteIn;
-
- /* Data stream to write to server. */
- DataOutputStream remoteOut;
-
- private int port = 5001;
- private Socket sock;
- private String server = null;
- // name of the remote server (null if on same machine)
-
- /**
- * Establish a connection with the database server.
- * @exception IOException Thrown if there's trouble
- * connecting to the server
- */
- void establishConnection() throws IOException {
- try {
- InetAddress serverAddr = InetAddress.getByName(server);
- sock = new Socket(serverAddr.getHostName(), port, true);
-
- remoteIn = new DataInputStream(sock.getInputStream());
- remoteOut = new DataOutputStream(sock.getOutputStream());
-
- } catch (IOException e) {
- System.out.println(e.getMessage() +
- ": Failed to connect to server.");
- throw e;
- }
-
- }
-
- /*
- * Close the connection with the database server.
- */
- void closeConnection() {
- try {
- if (remoteOut != null) {
- remoteOut.close();
- remoteOut = null;
- }
-
- if (remoteIn != null) {
- remoteIn.close();
- remoteIn = null;
- }
-
- } catch (IOException x) {
- System.out.println(x.getMessage());
- } finally {
- try {
- if (sock != null) {
- sock.close();
- sock = null;
- }
- } catch (IOException x) {
- System.out.println(x.getMessage());
- }
- }
- }
-
- int[] getOpenSeats() throws IOException {
- establishConnection();
- remoteOut.writeInt(Server.OP_GET_OPEN_SEATS);
- int numSeats = remoteIn.readInt();
- int[] seats = new int[numSeats];
- for (int i = 0; i < numSeats; i++)
- seats[i] = remoteIn.readInt();
-
- closeConnection();
- return seats;
- }
-
- String[] getPassengerList() throws IOException {
- establishConnection();
- remoteOut.writeInt(Server.OP_GET_PASSENGER_LIST);
- int numPassengers = remoteIn.readInt();
- String[] passengers = new String[numPassengers];
- for (int i = 0; i < numPassengers; i++)
- passengers[i] = remoteIn.readUTF();
-
- closeConnection();
- return passengers;
- }
-
- int reservePassenger(String name, int seat) throws IOException{
- establishConnection();
- remoteOut.writeInt(Server.OP_MAKE_RESERVATION);
- remoteOut.writeUTF(name);
- remoteOut.writeInt(seat);
- int rv = remoteIn.readInt();
- closeConnection();
- return rv;
- }
-
- int deletePassenger(String name) throws IOException {
- establishConnection();
- remoteOut.writeInt(Server.OP_DELETE);
- remoteOut.writeUTF(name);
- int rv = remoteIn.readInt();
- closeConnection();
- return rv;
- }
-
- int getSeat(String name) throws IOException {
- establishConnection();
- remoteOut.writeInt(Server.OP_GET_SEAT);
- remoteOut.writeUTF(name);
- int seat = remoteIn.readInt();
- closeConnection();
- return seat;
- }
-
- protected void finalize() throws Throwable {
- super.finalize();
- closeConnection();
- }
-
- }
-